package com.zhang.spark_1.spark_core.operator.transform

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * @title:
 * @author: zhang
 * @date: 2021/12/5 18:51 
 */
object Spark06_RDD_Operator_Transform_Test2 {

  def main(args: Array[String]): Unit = {
    //获取spark的连接
    val conf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("operator")
    val sc: SparkContext = new SparkContext(conf)
    //TODO groupBy 小功能：从服务器日志数据apache.log中获取每个时间段访问量。

    val rdd: RDD[String] = sc.textFile("datas/apache.log")

    val rddMap: RDD[(String, Int)] = rdd.map(
      line => {
        val datas = line.split(" ")
        val time = datas(3)
        val hour: Array[String] = time.split(":")
        (hour(1), 1)
      }
    )//.reduceByKey(_+_)
    val rddGroup: RDD[(String, Iterable[(String, Int)])] = rddMap.groupBy(_._1)

    //val value: RDD[(String, Int)] = rddGroup.map(kv => (kv._1, kv._2.size))
    //偏函数写法
    val value: RDD[(String, Int)] = rddGroup.map {
      case (str, iter) => (str, iter.size)
    }
    value.collect().foreach(println)
    sc.stop()
  }
}
